package com.testplatfrom.project.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.testplatfrom.common.core.exception.project.ProjectException;
import com.testplatfrom.common.core.utils.bean.BeanUtils;
import com.testplatfrom.common.security.utils.SecurityUtils;
import com.testplatfrom.project.domain.TestplatfromProject;
import com.testplatfrom.project.domain.TestplatfromProjectmember;
import com.testplatfrom.project.domain.dto.member.TestplatfromProjectmemberAdd;
import com.testplatfrom.project.domain.dto.project.*;
import com.testplatfrom.project.mapper.TestplatfromProjectMapper;
import com.testplatfrom.project.service.TestplatfromProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.testplatfrom.project.service.TestplatfromProjectmemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 杨森
 * @since 2023-06-26
 */
@Service
public class TestplatfromProjectServiceImpl extends ServiceImpl<TestplatfromProjectMapper, TestplatfromProject> implements TestplatfromProjectService {

    @Autowired
    private TestplatfromProjectMapper testplatfromProject;

    @Autowired
    private TestplatfromProjectmemberService testplatfromProjectmemberService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addTestplatfromProject(TestplatfromProjectAdd testplatfromProjectAdd) {
        //查询项目名称是否存在
        LambdaQueryWrapper<TestplatfromProject> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<TestplatfromProject> testplatfromProjectList = list(lambdaQueryWrapper.eq(TestplatfromProject::getTestplatfromProjectDescribe, testplatfromProjectAdd.getTestplatfromProjectDescribe()));
        if (testplatfromProjectList.size() > 0) {
            throw new ProjectException("项目名称已存在");
        }
        TestplatfromProject testplatfromProject = new TestplatfromProject();
        BeanUtils.copyProperties(testplatfromProjectAdd, testplatfromProject);
        testplatfromProject.setCreateBy(SecurityUtils.getUsername());
        testplatfromProject.setCreateTime(new Date());
        testplatfromProject.setUserId(SecurityUtils.getUserId());
        testplatfromProject.setDeptId(SecurityUtils.getLoginUser().getSysUser().getDeptId());
        save(testplatfromProject);
        //将项目的创建人添加到项目成员中
        ArrayList<Long> usedIdsList = new ArrayList<>();
        usedIdsList.add(SecurityUtils.getUserId());
        return testplatfromProjectmemberService.addTestplatfromProjectMember(TestplatfromProjectmemberAdd.builder().testplatfromProjectId(testplatfromProject.getTestplatfromProjectId())
                .userIds(usedIdsList).build());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateTestplatfromProject(TestplatfromProjectEdit testplatfromProjectEdit) {
        //查询项目是否存在
        TestplatfromProject testplatfromProject = getById(testplatfromProjectEdit.getTestplatfromProjectId());
        if (ObjectUtil.isNull(testplatfromProject)) {
            throw new ProjectException("项目不存在");
        }
        //查询是否重名
        LambdaQueryWrapper<TestplatfromProject> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<TestplatfromProject> testplatfromProjectList = list(lambdaQueryWrapper.eq(TestplatfromProject::getTestplatfromProjectDescribe, testplatfromProjectEdit.getTestplatfromProjectDescribe())
                .ne(TestplatfromProject::getTestplatfromProjectId, testplatfromProjectEdit.getTestplatfromProjectId()));
        if (testplatfromProjectList.size() > 0) {
            throw new ProjectException("项目名称已存在");
        }
        testplatfromProject.setTestplatfromProjectDescribe(testplatfromProjectEdit.getTestplatfromProjectDescribe());
        testplatfromProject.setRemark(testplatfromProjectEdit.getRemark());
        testplatfromProject.setUpdateBy(SecurityUtils.getUsername());
        testplatfromProject.setUpdateTime(new Date());
        return updateById(testplatfromProject);
    }

    @Override
    public TestplatfromProjectPageVo listTestplatfromProject(TestplatfromProjectPage testplatfromProjectPage) {
        LambdaQueryWrapper<TestplatfromProject> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(ObjectUtil.isNotEmpty(testplatfromProjectPage.getTestplatfromProjectDescribe()), TestplatfromProject::getTestplatfromProjectDescribe, testplatfromProjectPage.getTestplatfromProjectDescribe())
                .like(ObjectUtil.isNotEmpty(testplatfromProjectPage.getCreateBy()), TestplatfromProject::getCreateBy, testplatfromProjectPage.getCreateBy())
                .orderByDesc(TestplatfromProject::getCreateBy);
        //如果是admin则返回全部项目
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            //判断项目该人员包括的项目
            List<TestplatfromProjectmember> testplatfromProjectmembers = testplatfromProjectmemberService.list(new LambdaQueryWrapper<TestplatfromProjectmember>().eq(TestplatfromProjectmember::getUserId, SecurityUtils.getUserId()));
            //若list不为空
            if (!CollectionUtils.isEmpty(testplatfromProjectmembers)){
                List<String> collect = testplatfromProjectmembers.stream().map(TestplatfromProjectmember::getTestplatfromProjectId).collect(Collectors.toList());
                lambdaQueryWrapper.in(TestplatfromProject::getTestplatfromProjectId,collect);
            }else {
                //若为空 则该用户没有项目权限 则返回空列表
                return TestplatfromProjectPageVo.builder()
                        .size(testplatfromProjectPage.getSize())
                        .current(testplatfromProjectPage.getCurrent())
                        .total(testplatfromProjectPage.getTotal())
                        .build();
            }
        }
        TestplatfromProjectPage projectPage = page(testplatfromProjectPage, lambdaQueryWrapper);
        TestplatfromProjectPageVo projectPageVo = TestplatfromProjectPageVo.builder()
                .size(projectPage.getSize())
                .current(projectPage.getCurrent())
                .total(projectPage.getTotal())
                .build();
        //按项目分人员集合
        Map<String, List<TestplatfromProjectmember>> listMap = testplatfromProjectmemberService.list().stream().collect(Collectors.groupingBy(TestplatfromProjectmember::getTestplatfromProjectId));
        ArrayList<TestplatfromProjectVo> arrayList = new ArrayList<>();
        for (TestplatfromProject record : projectPage.getRecords()) {
            TestplatfromProjectVo testplatfromProjectVo = TestplatfromProjectVo.builder().build();
            BeanUtils.copyProperties(record, testplatfromProjectVo);
            if (!CollectionUtils.isEmpty(listMap.get(testplatfromProjectVo.getTestplatfromProjectId()))){
                testplatfromProjectVo.setIntegerNumber(listMap.get(testplatfromProjectVo.getTestplatfromProjectId()).size());
            }else {
                testplatfromProjectVo.setIntegerNumber(0);
            }
            arrayList.add(testplatfromProjectVo);
        }
        projectPageVo.setRecords(arrayList);
        return projectPageVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteTestplatfromProject(String testplatfromProjectId) {
        return removeById(testplatfromProjectId);
    }

    @Override
    public TestplatfromProject getDetailTestplatfromProject(String testplatfromProjectId) {
        TestplatfromProject testplatfromProject = this.testplatfromProject.selectById(testplatfromProjectId);
        if (ObjectUtil.isNull(this.testplatfromProject)){
            throw new ProjectException("项目不存在");
        }
        return testplatfromProject;
    }

    @Override
    public List<TestplatfromProjectSelectVO> selectTestplatfromProject() {
        //若admin则查询出所有项目 非admin则为当前下成员的项目
        LambdaQueryWrapper<TestplatfromProject> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //如果是admin则返回全部项目
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            //判断项目该人员包括的项目
            List<TestplatfromProjectmember> testplatfromProjectmembers = testplatfromProjectmemberService.list(new LambdaQueryWrapper<TestplatfromProjectmember>().eq(TestplatfromProjectmember::getUserId, SecurityUtils.getUserId()));
            //若list不为空
            if (!CollectionUtils.isEmpty(testplatfromProjectmembers)){
                List<String> collect = testplatfromProjectmembers.stream().map(TestplatfromProjectmember::getTestplatfromProjectId).collect(Collectors.toList());
                lambdaQueryWrapper.in(TestplatfromProject::getTestplatfromProjectId,collect);
            }else {
                //若为空 则该用户没有项目权限 则返回空列表
                return new ArrayList<>();
            }
        }
        List<TestplatfromProject> testplatfromProjectList = this.list(lambdaQueryWrapper);
        ArrayList<TestplatfromProjectSelectVO> testplatfromProjectSelectVOS = new ArrayList<>();
        for (TestplatfromProject project : testplatfromProjectList) {
            TestplatfromProjectSelectVO testplatfromProjectSelectVO = TestplatfromProjectSelectVO.builder().testplatfromProjectId(project.getTestplatfromProjectId())
                    .testplatfromProjectDescribe(project.getTestplatfromProjectDescribe())
                    .build();
            testplatfromProjectSelectVOS.add(testplatfromProjectSelectVO);
        }
        return testplatfromProjectSelectVOS;
    }
}
